//
//  SubmitOrderViewController.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/12.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "SubmitOrderViewController.h"

//跳转界面
#import "PayOrderViewController.h"//支付订单
#import "ReceiveController.h"
//自定义视图
#import "SubmitOrderTopView.h"
#import "SubmitOrderBottomView.h"


//自定义cell
#import "SubmitAddressNoTableViewCell.h"//没有收货地址
#import "SubmitAddressTableViewCell.h"//有收货地址
#import "SubmitOrderGoodsTableViewCell.h"//商品信息


//NSUserDefults
#import "CacheUserInfo.h"

@interface SubmitOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)SubmitOrderTopView *TopView;
@property(nonatomic,strong)SubmitOrderBottomView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)CacheUserInfo *userInfo;
@property(nonatomic,assign)NSInteger price;
@property(nonatomic,assign)NSInteger allCost;


@end

@implementation SubmitOrderViewController

static NSString *SubmitAddressNoTableViewCellIden = @"SubmitAddressNoTableViewCellIden";
static NSString *SubmitAddressTableViewCellIden = @"SubmitAddressTableViewCellIden";
static NSString *SubmitOrderGoodsTableViewCellIden = @"SubmitOrderGoodsTableViewCellIden";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self load_UI];
    
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark ----- load_UI
-(void)load_UI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.userInfo = [CacheUserInfo sharedManager];
    
    self.price = 1980;
//    self.allCost = self.price * [self.userInfo.good_num integerValue] + [self.userInfo.good_freight integerValue];
    
    [self TopView];
    
    [self bottomView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SubmitAddressNoTableViewCell" bundle:nil] forCellReuseIdentifier:SubmitAddressNoTableViewCellIden];
    [self.tableView registerNib:[UINib nibWithNibName:@"SubmitAddressTableViewCell" bundle:nil] forCellReuseIdentifier:SubmitAddressTableViewCellIden];
    [self.tableView registerNib:[UINib nibWithNibName:@"SubmitOrderGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:SubmitOrderGoodsTableViewCellIden];
}


#pragma mark --------- tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            return 1;
        }
            break;
        case 1:
        {
            return 1;
        }
            break;
            default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            return 104 * FitH;

        }
            break;
        case 1:
        {
            return 196 * FitH;
        }
            break;
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 5 * FitH;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    switch (indexPath.section)
    {
        case 0:
        {
            SubmitAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SubmitAddressTableViewCellIden forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 1:
        {
            SubmitOrderGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SubmitOrderGoodsTableViewCellIden forIndexPath:indexPath];
            cell.model = self.model;
            
//            NSInteger price = [self.model.good_price integerValue] ;
//            NSInteger price = 1980;
//            cell.userAllCost = price * [_userInfo.good_num integerValue] + [_userInfo.good_freight integerValue];
            cell.userAllCost = self.allCost;//总费用
            cell.goodCount = [_userInfo.good_num integerValue];//商品数量
            cell.goodFreight = [_userInfo.good_freight integerValue];//运费
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
            default:
            return nil;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            DLog(@"地址添加修改");
            
            ReceiveController * rvc = [[ReceiveController alloc]init];
            
            [self.navigationController pushViewController:rvc animated:YES];
            
        }
            break;
        case 1:
        {
            DLog(@"商品详情");
        }
            break;
        default:
            break;
    }
}


#pragma amrk ---- lazy loading
-(SubmitOrderTopView *)TopView
{
    WeakObj(self);
    if (!_TopView)
    {
        _TopView = [[SubmitOrderTopView alloc]init];
//        _TopView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_TopView];
        [_TopView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(20);
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.height.equalTo(@44);
        }];
        
        _TopView.backButtonClickedHandler = ^{
        
            [weakself.navigationController popViewControllerAnimated:YES];
        };
        
    }
    return _TopView;
}

-(SubmitOrderBottomView *)bottomView
{
    WeakObj(self);
    if (!_bottomView) {
        _bottomView = [[SubmitOrderBottomView alloc]init];
        [self.view addSubview:_bottomView];
        
//        NSInteger price = 1980;
//        NSInteger allCost = price * [_userInfo.good_num integerValue];
        
        self.allCost = self.price * [self.userInfo.good_num integerValue] + [self.userInfo.good_freight integerValue];
//        NSInteger freight = [self.userInfo.good_freight integerValue];
        
        
        _bottomView.allCost = _allCost  ;
        _bottomView.goodNum = [self.userInfo.good_num integerValue];
        
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.bottom.equalTo(self.view.mas_bottom);
            make.height.equalTo(@67);
        }];
        _bottomView.buttonClickedHandler = ^{
        
            DLog(@"提交订单");
            PayOrderViewController *pvc = [[PayOrderViewController alloc]initWithNibName:@"PayOrderViewController" bundle:nil];
            

            pvc.userAllCost = weakself.allCost;
            pvc.model = weakself.model;
            
            [weakself.navigationController pushViewController:pvc animated:YES];
            
        
            
        };
    }
    return _bottomView;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

-(void)viewWillDisappear:(BOOL)animated

{
    [super viewWillDisappear:animated];
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
